﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace System.Work.ClientEngine.FPXH.TestApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        IFPXHClient _client = null;

        private void btnConnect_Click(object sender, EventArgs e)
        {
            btnDisconnect_Click(sender, e);
            _client = new FPXHClient(txtLocalIP.Text.Trim(), int.Parse(txtLocalPort.Text.Trim()), txtPLCIP.Text.Trim(), int.Parse(txtPLCPort.Text.Trim()));
            _client.OnClientClosed += _client_OnClientClosed;
            _client.OnClientConnected += _client_OnClientConnected;
            _client.OnClientError += _client_OnClientError;
            _client.OnErrorCallback += _client_OnErrorCallback;
            _client.OnReadCallback += _client_OnReadCallback;
            _client.OnWriteCallback += _client_OnWriteCallback;
            _client.Connect();
        }

        #region 客户端事件

        private void AddMsg(string code)
        {
            Action ac = () =>
            {
                lstRunning.Items.Add(string.Format("{0} {1}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), code));
            };
            if (this.lstRunning.InvokeRequired)
                this.lstRunning.BeginInvoke(ac);
            else
                ac();
        }

        private void _client_OnWriteCallback(object sender, WriteDataEventArgs e)
        {
            AddMsg(string.Format("write {0}", e.WriteOK ? "success" : "failed:" + e.DebugMsg));
        }

        private void _client_OnReadCallback(object sender, ReadDataEventArgs e)
        {
            if (string.IsNullOrEmpty(e.DebugMsg))
            {
                AddMsg("read success,results:");
                for (int i = 0; i < e.Value.Length; i++)
                    AddMsg(string.Format("addressIndex={0},value={1}", i, e.Value[i]));
            }
            else
                AddMsg("read failed:" + e.DebugMsg);
        }

        private void _client_OnErrorCallback(object sender, ErrorDataEventArgs e)
        {
            AddMsg("error" + e.ErrorCode + e.ErrorMsg);
        }

        private void _client_OnClientError(object sender, EventArgs e)
        {
            AddMsg("client error");
        }

        private void _client_OnClientConnected(object sender, EventArgs e)
        {
            AddMsg("client connect");
        }

        private void _client_OnClientClosed(object sender, EventArgs e)
        {
            AddMsg("client disconnect");
        }
        #endregion

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            if (_client != null)
            {
                _client.OnClientClosed -= _client_OnClientClosed;
                _client.OnClientConnected -= _client_OnClientConnected;
                _client.OnClientError -= _client_OnClientError;
                _client.OnErrorCallback -= _client_OnErrorCallback;
                _client.OnReadCallback -= _client_OnReadCallback;
                _client.OnWriteCallback -= _client_OnWriteCallback;
                _client.Disconnect();
                _client.Dispose();
                _client = null;
            }
        }

        private bool CheckClintIsNotConnected()
        {
            bool br = _client == null || !_client.IsConnected;
            if (br)
                MessageBox.Show("请先连接PLC");
            return br;
        }

        private void btnReadS_Click(object sender, EventArgs e)
        {
            if (CheckClintIsNotConnected()) return;
            if (string.IsNullOrEmpty(txtReadStartAdd.Text))
            {
                MessageBox.Show("请输入起始地址");
                return;
            }
            _client.Read(txtReadStartAdd.Text.Trim());
        }

        private void btnReadM_Click(object sender, EventArgs e)
        {
            if (CheckClintIsNotConnected()) return;
            if (string.IsNullOrEmpty(txtReadStartAdd.Text))
            {
                MessageBox.Show("请输入起始地址");
                return;
            }
            if (string.IsNullOrEmpty(txtReadEndAdd.Text))
            {
                MessageBox.Show("请输入终止地址");
                return;
            }
            int count = 0;
            if (!int.TryParse(txtReadAddCount.Text.Trim(), out count) || count < 1)
            {
                MessageBox.Show("请输入读取地址的数量");
                return;
            }
            _client.Read(txtReadStartAdd.Text.Trim(), txtReadEndAdd.Text.Trim(), count);
        }

        private void btnWriteS_Click(object sender, EventArgs e)
        {
            if (CheckClintIsNotConnected()) return;
            if (string.IsNullOrEmpty(txtWriteStartAdd.Text))
            {
                MessageBox.Show("请输入起始地址");
                return;
            }
            int value = 0;
            if (!int.TryParse(txtWriteValue.Text.Trim(), out value))
            {
                MessageBox.Show("请输入写入地址的数值");
                return;
            }
            _client.Write(txtWriteStartAdd.Text.Trim(), value);
        }

        private void btnWriteM_Click(object sender, EventArgs e)
        {
            if (CheckClintIsNotConnected()) return;
            MessageBox.Show("稍等");
        }
    }
}
